package hsl.study.v3;


import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author HSL
 * @version 1.0
 * @data 2025/11/17 11:23
 * @desc JDK 动态代理实现
 */

public class JdkProxyHandler implements InvocationHandler {
    private Object target;

    public JdkProxyHandler(Object target) {
        this.target = target;
    }

    public Object getProxyInstance() {
        return Proxy.newProxyInstance(
                target.getClass().getClassLoader(),
                target.getClass().getInterfaces(),
                this
        );
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("[JDK代理] 调用方法：" + method.getName());
        Object result = method.invoke(target, args);
        System.out.println("[JDK代理] 方法执行完毕");
        return result;
    }
}
